# -*- coding: utf-8 -*-
"""
Created on Mon Nov 23 17:59:52 2020
("http://www.bing.com")
waits
@author: andrew
"""
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Expected Conditions
# title_is
# title_contains
# presence_of_element_located
# visibility_of_element_located
# visibility_of
# presence_of_all_elements_located
# text_to_be_present_in_element
# text_to_be_present_in_element_value
# frame_to_be_available_and_switch_to_it
# invisibility_of_element_located
# element_to_be_clickable
# staleness_of
# element_to_be_selected
# element_located_to_be_selected
# element_selection_state_to_be
# element_located_selection_state_to_be
# alert_is_present

driver = webdriver.Chrome('./diver/chromedriver')
# Implicit Waits隐性等待
driver.implicitly_wait(5) 
driver.get("http://www.bing.com")
print(driver.title)
driver.maximize_window()


#1 定位设置，鼠标点击设置
# <a id="id_sc" class="idp_ham hphbtop" aria-label="设置和快速链接" aria-expanded="false" aria-controls="id_hbfo" aria-haspopup="true" role="button" tabindex="0" href="javascript:void(0);" h="ID=SERP,5017.1"></a>
element = driver.find_element(By.ID, 'id_sc')
print(element.get_attribute('class'))
element.click()
time.sleep(5)


# 显性等待Explicit Waits
try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, "id_sc"))
    )
    element.click()
except :
    print("No element is id_sc By.ID")
    
    # driver.quit()




# element.click()
# time.sleep(5)
# 弹出下拉菜单后，点击中文“设置”，会弹出下拉菜单
# XPATH： /html/body/div[2]/div/div[3]/header/div[2]/div/span[2]/div/div[1]/div[1]/div
# <div class="hb_titlerow">
#     <div class="hbic_col">  <span class="hbic_setting"></span>   </div>
#     <div class="hb_title_col">设置     </div>
#     <div class="hb_value_col">  <span class="hbic_chevdown"></span> <span class="hbic_chevup b_hide"></span>
#     </div>
# </div>
try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH,'/html/body/div[2]/div/div[3]/header/div[2]/div/span[2]/div/div[1]/div[1]/div'))
    )
    print(element.get_attribute('class'))
    element.click()
except :
    print("No element is 设置")


# 弹出下拉菜单后，点击“语言”菜单项
# XPATH：/html/body/div[2]/div/div[3]/header/div[2]/div/span[2]/div/div[1]/div[2]/a[1]
# <a class="hb_section" tabindex="0" role="menuitem" href="/account/general?ru=https%3a%2f%2fwww.bing.com%2f&amp;FORM=O2HV65&amp;id=language_section#language-section" h="ID=SERP.5018,5366.1"><div class="hb_titlerow"><div class="hbic_col"></div><div class="hb_title_col">语言</div><div class="hb_value_col"></div></div><div class="hb_detrow">中文(简体)</div></a>
try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH,'/html/body/div[2]/div/div[3]/header/div[2]/div/span[2]/div/div[1]/div[2]/a[1]'))
    )
    print(element.get_attribute('class'))
    element.click()
except :
    print("No element is 语言")




#定位所有语言列表，该列表以<li>形式存放为3列，然后定位第一列，并显示其中内容
print(driver.current_url)
# <ul class="b_vList b_divsec">
#     <li data-priority="">
        # <a href="https://www.bing.com/account/action?scope=web&amp;setlang=ku-arab&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5063.1">中库尔德语
        # </a>
        # <div>‫کوردیی ناوەڕاست‬
        # </div>
#     </li>
#     <li data-priority="">
#         <a href="https://www.bing.com/account/action?scope=web&amp;setlang=zh-hans&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5119.1">中文(简体)
#             </a>
#             <div>中文(简体)</div>
#         </li>
      # <li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=zh-hant&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5120.1">中文(繁体)</a><div>中文(繁體)</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=da&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5031.1">丹麦语</a><div>dansk</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=uk&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5112.1">乌克兰语</a><div>українська</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=uz-latn&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5114.1">乌兹别克语(拉丁语)</a><div>oʻzbekcha</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=ur&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5113.1">乌尔都语</a><div>‫اردو‬</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=nb-no&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5076.1">书面挪威语</a><div>norsk bokmål</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=hy&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5051.1">亚美尼亚语</a><div>Հայերեն</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=ig&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5053.1">伊博语</a><div>Igbo</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=ru&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5091.1">俄语</a><div>русский</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=bg&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5023.1">保加利亚语</a><div>Български</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=sd-arab&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5093.1">信德语(阿拉伯语)</a><div>‫سنڌي‬</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=si&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5094.1">僧伽罗语</a><div>සිංහල</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=quz&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5089.1">克丘亚语</a><div>Runasimi</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=hr&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5049.1">克罗地亚语</a><div>hrvatski</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=is&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5054.1">冰岛语</a><div>Íslenska</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=chr-cher&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5028.1">切罗基语(切罗基)</a><div>ᏣᎳᎩ</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=gl&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5044.1">加利西亚语</a><div>Galego</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=ca&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5026.1">加泰罗尼亚语</a><div>català</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=hu&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5050.1">匈牙利语</a><div>magyar</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=nso-za&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5080.1">北索托语</a><div>Sesotho sa Leboa</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=af&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5017.1">南非荷兰语</a><div>Afrikaans</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=kn&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5060.1">卡纳达语</a><div>ಕನ್ನಡ</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=rw&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5092.1">卢旺达语</a><div>Kinyarwanda</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=lb&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5065.1">卢森堡语</a><div>Lëtzebuergesch</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=hi&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5048.1">印地语</a><div>हिन्दी</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=id&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5052.1">印度尼西亚语</a><div>Indonesia</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=gu&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5045.1">古吉拉特语</a><div>ગુજરાતી</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=kk&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5058.1">哈萨克语</a><div>қазақ тілі</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=tk&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5107.1">土库曼语(拉丁语)</a><div>türkmen dili</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=tr&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5109.1">土耳其语</a><div>Türkçe</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=quc&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5088.1">基切语</a><div>K'iche'</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=tg-cyrl&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5104.1">塔吉克语(西里尔文)</a><div>Тоҷикӣ</div></li><li data-priority=""><a href="https://www.bing.com/account/action?scope=web&amp;setlang=sr-latn-rs&amp;FORM=W5WA&amp;uid=9EE0FEB3&amp;sid=2333FA2D701368823D1CF5B571BF690E" h="ID=SERP,5099.1">塞尔维亚语(拉丁语)</a><div>srpski</div></li></ul>
# /html/body/div[1]/div[2]/div[2]/form/div[3]/div[2]/div[1]/div/div[2]/div/div[3]/div
# /html/body/div[1]/div[2]/div[2]/form/div[3]/div[2]/div[1]/div/div[2]/div/div[3]/div/div[1]/ul
try:
    element = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH,'/html/body/div[1]/div[2]/div[2]/form/div[3]/div[2]/div[1]/div/div[2]/div/div[3]/div/div[1]/ul'))
    )
except :
    print("No element is 语言列表")
element.click()

try:
    options = WebDriverWait(element, 10).until(
        EC.presence_of_all_elements_located((By.TAG_NAME,'li'))
    )
except :
    print("No element is <li>")

# 显示所有的<li>
# try:
#     options = WebDriverWait(driver, 10).until(
#         EC.presence_of_all_elements_located((By.TAG_NAME,'li'))
#     )
# except :
#     print("No element is <li>")


print("Languages included:", len(options))
for opt in options:
    print(opt.get_attribute('innerHTML'))

time.sleep(5)
# driver.quit()

